package com.cx.controller;

import com.cx.mapper.StudentMapper;
import com.cx.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * @auther: Centaury
 * @Date:2023/3/22 - 03 - 22 - 17:05
 */
@Controller
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    // 跳转到增加学生表单
    @GetMapping("/student")
    public String toAddPage(Model model){
        return "student/add";
    }
    // 增
    @PostMapping("/addStu")
    public String addStudent(Student student){
        int rows = studentMapper.addStudent(student);
        System.out.println("rows==>"+rows);
        return "redirect:/queryStudentList";
    }

    //查
    @GetMapping("/queryStudentList")
    public String queryStudentList(Model model){
        List<Student> students = studentMapper.queryStudentList();
        model.addAttribute("students",students);
        return "/student/list";
    }

    // 删
    @GetMapping("/deleteStu/{id}")
    public String deleteStudent(@PathVariable("id")Integer id){
        int rows = studentMapper.deleteStudent(id);
        System.out.println("rows==>"+rows);
        return "redirect:/queryStudentList";
    }
    // 去学生的修改页面
    @GetMapping("/student/{id}")
    public String toUpdateEmp(@PathVariable("id")Integer id,Model model){
        // 查出原来的数据
        Student student = studentMapper.queryStudentById(id);
        model.addAttribute("student",student);
        return "student/update";
    }
    // 改
    @PostMapping("/updateStu")
    public String updateStudent(Student student){
        int rows = studentMapper.updateStudent(student);
        System.out.println("rows==>"+rows);
        return "redirect:/queryStudentList";
    }
}
